function out = readoriginoms2020(filename, CaV, varargin)

% Constants
%----------

lens_mag = 1; %1x lens
binning = 3; %should be odd number (1,3,5,7,...)


% Input argument checking
%------------------------
out = [];
if nargin == 0
    [file,filepath] = uigetfile({'*.gsd','OMS high speed camera simple binary file (*.gsd)'; ...
        '*.*','All Files (*.*)'}, 'Read OMS High Speed Camera Simple Binary File');
    if file == 0
        return;
    end
    filename = [filepath,file];
end

if ~exist(filename,'file')
%    filename = [filename,'.oms'];  % try .oms extension
    if ~exist(filename,'file')
        error('The file name you entered could not be found.');
    end
end

fid = fopen(filename,'rb','l'); %SJ changed: 'b' Big-endian ordering, 'l' little-endian ordering


backslash = find(filename == '\');
if isempty(backslash)
    out.name = filename;
else
    out.name = filename(backslash(end)+1:end);
end

%Sj add V2020
%SJ int16:16bit signed binary
temp1 = fread(fid,[1 128],'int16')'; % read 256 byte header information
xsize = fread(fid,1,'int16')'; %SJ 256-257
ysize = fread(fid,1,'int16')'; %SJ 258-259
temp2 = fread(fid,[1 4],'int16')'; % read 256 byte header information
totalframenumber = fread(fid,1,'int16'); %SJ 268-269 '-1; %SJ
temp3 = fread(fid,[1 7],'int16')'; % read 270-283 byte header information

samplingTime = fread(fid,1,'single')'; %SJ (msec) 284-287
fs = 1000/samplingTime; %SJ (number of frames per sec)

temp3 = fread(fid, 342,'int16')'; % read 288-971 byte header information
% 
% %size FOV
% sizeFOV = 10/lens_mag; %mm
% 
% 
bgimagedata = fread(fid, [xsize ysize],'int16'); %SJ background image data


%temp4 = fread(fid, xsize*ysize, 'int16')'; %SJ read first data set


for i=1:totalframenumber
%for i=1:3
    i
    if(i==1)
        differentialdata = zeros(xsize, ysize, totalframenumber);
    end
    differentialdata(:,:,i) = fread(fid, [xsize ysize], 'int16'); %SJ background image data
    %realdata(i,:) = bgimagedata + differentialdata(i,:);
end
fclose(fid);

for i=1:totalframenumber
    realdata(:,:,i) = CaV*differentialdata(:,:,i)./bgimagedata;
end



% 
% %Software binning function SJ
% %binning = 1;
% if (binning>1)&&(binning<xsize)
%     binningrealdata = realdata;
%     hbinning = floor(binning/2);
%     for j=1:ysize
%         for i=1:xsize
%             elementnumber = 0;
%             sumelement = zeros(size(realdata,1),1);
%             for binny = (j-hbinning):(j+hbinning);
%                 for binnx = (i-hbinning):(i+hbinning);
%                     if(binny>0)&&(binny<ysize+1)
%                         if(binnx>0)&&(binnx<xsize+1)
%                             elementnumber = elementnumber + 1;
%                             sumelement = sumelement + realdata(:,(binny-1)*xsize + binnx);
%                         end
%                     end
%                 end
%             end
%             binningrealdata(:,(j-1)*xsize + i) = sumelement/elementnumber;
%         end
%     end
%     realdata = binningrealdata;
% end
% 
% knownPacingStart = knownPacing(1);
% knownPacingStop = knownPacing(length(knownPacing));
% nPulse = length(knownPacing);
%     
% %widthPacing = round((knownPacingStop-knownPacingStart)/(nPulse-1));
% widthPacing = min(diff(knownPacing));
% 
% trig = knownPacing;  
% 
% 
% 
% for ii=1:nPulse-1
%     if(ii==1)
%         averagedData=zeros(widthPacing,xsize*ysize);
%     end
%     for kk=1:xsize*ysize
%         averagedData(:,kk) = averagedData(:,kk) + realdata((trig(ii):(trig(ii)+widthPacing-1)), kk);
%     end
%     if(ii==(nPulse-1))
%         averagedData = averagedData/(nPulse-1);
%     end
%     for kk=1:xsize*ysize
%         [maxValue(ii,kk), maxI(ii,kk)] = max(realdata((trig(ii):(trig(ii)+widthPacing-1)), kk));
%     end
%     if(ii==(nPulse-1))
%         averagedI = mean(maxI);
%     end
% end
% 
% 
out.fs = fs;
out.samplingTime = samplingTime;
out.data = realdata;
out.differentialdata = differentialdata;
out.bgimagedata = bgimagedata;
%out.averageddata = averagedData;
out.xsize = xsize;
out.ysize = ysize;
out.totalframenumber = totalframenumber;
% 
% out.widthPacing = widthPacing;
% out.nPulse = nPulse;
% out.maxValue = maxValue;
% out.maxI = maxI;
% out.averagedI = averagedI;
% 
% % out.coordinates = coordinates;
% % out.activationIndex = activationIndex';
% % out.datasize = datasize;
% % out.istart = istart;
% % out.istop = istop;
% % out.meaniData = meaniData;
% % out.t_plot = t_plot;
% % out.finaliData = finaliData;
% % out.normiData = normiData;
% % out.APD50 = APD50;
% % out.APD90 = APD90;
% % out.APMax = APMax;